//
//  MTNavViewController.swift
//  MTWeiBo
//
//  Created by user on 2017/7/22.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class MTNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //设置pop手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }

    //监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        
        //在push之前,先隐藏tabbar
        if viewControllers.count > 0 {
            
            //影藏
            viewController.hidesBottomBarWhenPushed = true
        }
        
       // print(viewControllers.count)
        super.pushViewController(viewController, animated: true)
       // print(viewControllers.count)
        
        if viewControllers.count > 1 {
            var title: String = ""
            
            if viewControllers.count == 2 {
                title = viewControllers.first!.title!
            }else{
                title = "返回"
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(backAction),imageName: "navigationbar_back_withtext")
            viewController.navigationItem.title = "当前显示的是第\(viewControllers.count)个控制器"
        }
    }
    
    @objc private func backAction() {
        self.popViewController(animated: true)
    }
    
    //UIGestureRecognizerDelegate
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1{
            return false
        }
        
        return true
    }
    
}
